<!DOCTYPE html>
<html xmlns:layout="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
	layout:decorate="~{common/layout}">

<head>
	<meta charset="UTF-8">
	<title>血糖记录管理系统</title>
	<style scoped lang="less">

	</style>
</head>

<body>
	<div layout:fragment="fragment_body">
		<van-row>
			<van-nav-bar title="咨询记录" left-text="返回" left-arrow @click-left="onClickLeft" />
		</van-row>
		<van-empty v-if="guidances == null || guidances.length == 0" description="暂无咨询记录" ></van-empty>
		<van-row v-for="(guidance, index) in guidances" :key="index">
			<van-cell-group>
				<van-cell>
					<template #title>
						<span class="custom-title"
							v-if="accountInfo.accountRole == 1">用户咨询：{{guidance.send.accountNick}}</span>
						<span class="custom-title"
							v-if="accountInfo.accountRole == 0">咨询专家：{{guidance.receive.accountNick}}</span>
					</template>
					<template #default>
						<van-button plain size="small" type="danger" v-if="accountInfo.accountRole == 0"
							@click="delGuidance(guidance)">删除</van-button>
						<van-button plain size="small" type="warning" v-if="accountInfo.accountRole == 1"
							@click="replyGuidance(guidance)">回复</van-button>
					</template>
				</van-cell>
			</van-cell-group>
			<van-collapse v-model="activeNames" accordion>
				<van-collapse-item :title="guidance.guidanceText" :name="guidance.guidanceId">
					{{guidance.guidanceReply?guidance.guidanceReply:'未回复'}}
				</van-collapse-item>
			</van-collapse>
			<van-divider />
		</van-row>
		<!-- 弹框组件 -->
		<van-dialog v-model="showDialog" title="咨询回复" show-cancel-button show-confirm-button @close="showDialog = false"
			@confirm="saveReplyGuidance" :before-close='beforeclose'>
			<!-- 输入框 -->
			<van-form ref="form">
				<van-field v-model="guidanceReply" placeholder="请输入内容" :rules="[{ required: true}]" required rows="3"
					autosize type="textarea" maxlength="50" clearable></van-field>
			</van-form>
		</van-dialog>

	</div>
</body>
<script type="text/javascript" layout:fragment="fragment_script">
	let vueData = {
		activeMenu: 'my',
		guidances: [],
		activeNames: [],
		showDialog: false,
		guidanceId: '',
		guidanceReply: '',
	}
	let vueComputed = {

	}
	let vueMethods = {
		onClickLeft() {
			window.location.href = '/user/my'
		},
		getGuidances() {
			let that = this
			var url = '/user/getGuidancesBySend'
			if (that.accountInfo.accountRole === 1) {
				url = '/user/getGuidancesByReceive'
			}
			postRequst(url, {}, true).then(function (resp) {
				that.guidances = resp.rows
			})
		},
		delGuidance(guidance) {
			let that = this
			postRequst('/user/delGuidance', {guidanceId: guidance.guidanceId}, true).then(function (resp) {
				if (resp.status === 0) {
					that.getGuidances()
					that.$toast.success(resp.msg);
				} else {
					that.$toast(resp.msg);
				}
			})
		},
		replyGuidance(guidance) {
			this.showDialog = true
			this.guidanceId = guidance.guidanceId
			this.guidanceReply = guidance.guidanceReply
		},
		beforeclose(action, done) {
			let that = this
			if (action === 'confirm' && !that.guidanceReply) {
				this.showDialog = true;
				that.$toast('请填写');
				return done(false)
			} else {
				return done()
			}
		},
		saveReplyGuidance() {
			let that = this
			if(!that.guidanceReply){
				return false
			}
			postRequst('/user/replyGuidance', {guidanceId: that.guidanceId, guidanceReply: that.guidanceReply}, true)
				.then(function (resp) {
					if (resp.status === 0) {
						that.getGuidances()
						that.$toast.success(resp.msg);
					} else {
						that.$toast(resp.msg);
					}
				})
		},

	}
	function vueCreated() {
		let that = this
		setTimeout(function () {
			that.getGuidances()
		}, 100)
	}
</script>

</html>